/***************************************************************************
 创建者: 华磊
 开始时间: 2019.3.12
 copyright: (C) 华友高科
 修改说明: (每次有修改就添加一条，带有 修改人，修改时间，修改描述)
 example (1) hualei 2017.5.8 类的具体实现代码编写
 ***************************************************************************/
#include "canbusbattery.h"

#include <qsettings.h>
#include <qdebug.h>

CanbusBattery::CanbusBattery(QString fileNameIn, int aliasIn, int busNumberIn,
                             int deviceIdIn, CanbusDeviceDriver *canbusDriverIn)
{
    isInitialedOk=false;
    alias=aliasIn;
    busNumber=busNumberIn;
    deviceId=deviceIdIn;
    canbusDriver=canbusDriverIn;
    errorStatus=0;
    batteryCurrentCurrency=0;
    batteryLeftLightmodRatio=1.0;
    if(1==initial(fileNameIn))
    {
        isInitialedOk=true;
    }
}

int CanbusBattery::getBatteryInfo(BatteryInformation &batteryInfoOut)
{
    batteryInfoOut.batteryTempture=batteryTempture;
    batteryInfoOut.batterySumVoltage=batterySumVoltage;
    batteryInfoOut.batteryCurrentCurrency=batteryCurrentCurrency;
    batteryInfoOut.batteryLeftLightmodRatio=batteryLeftLightmodRatio;
    batteryInfoOut.batteryLeftLightmodVolume=batteryLeftLightmodVolume;
    batteryInfoOut.batteryCycleNumber=batteryCycleNumber;
    batteryInfoOut.batteryDesignVolume=batteryDesignVolume;
    batteryInfoOut.manufatureData=manufatureData;
    batteryInfoOut.balanceStatus=balanceStatus;
    batteryInfoOut.errorStatus=errorStatus;
    batteryInfoOut.softWareVersition=softWareVersition;
    batteryInfoOut.fet=fet;
    batteryInfoOut.batterySerialCount=batterySerialCount;
    batteryInfoOut.ntcCount=ntcCount;

    return 1;
}

int CanbusBattery::updateData()
{
    if(false==isInitialedOk)
    {
        return -2;
    }
    return processDataFrame();
}

int CanbusBattery::getDeviceAddressInfo(int &aliasOut, int &busNumberOut, int &deviceIdOut)
{
    aliasOut=alias;
    busNumberOut=busNumber;
    deviceIdOut=deviceId;
    return 1;
}

bool CanbusBattery::isInitialOkStatus()
{
    return isInitialedOk;
}

int CanbusBattery::initial(QString fileName)
{
    qDebug()<<"CanbusBattery initial ,file name="<<fileName;

    QVariant strConfigValue;
    QString firstTimeFlagStr = fileName;
    QString strConfigGroup = "";
    QString strConfigKey = "";

    QSettings settings(firstTimeFlagStr,QSettings::IniFormat);
    settings.setIniCodec("UTF-8");
    strConfigGroup = "GeneralInfo";
    settings.beginGroup(strConfigGroup);

    //vendorId
    strConfigKey = "vendorId";
    if(false==settings.contains(strConfigKey))
    {
        return -1;
    }
    strConfigValue = settings.value(strConfigKey);
    vendorId= (E_VENDOR_ID)strConfigValue.toInt();


    settings.endGroup();

    return 1;
}

int CanbusBattery::processDataFrame()
{
    QQueue<FrameData> userFrameBuffersOut;
    canbusDriver->getUserBuffer(busNumber,userFrameBuffersOut);

    switch (vendorId)
    {
    case E_VENDOR_ID_LI_TON_WEI:
        return processDataFrame_LiTonWei(userFrameBuffersOut);
        break;
    default:
        break;
    }
    return -1;
}

int CanbusBattery::processDataFrame_LiTonWei( const  QQueue<FrameData> &bufferIn)
{
    bool isReceived=false;

    //process data
    for(int i=0;i<bufferIn.size();i++)
    {
        if(bufferIn[i].frameId==0x20b)
        {
            batteryCycleNumber = (bufferIn[i].data[0] << 8) |
                                                            bufferIn[i].data[1];
            errorStatus = (bufferIn[i].data[4] << 8) | bufferIn[i].data[5];
            isReceived=true;
        }
        else if(bufferIn[i].frameId==0x20a)
        {
            // 电池组电流，数据两字节
            signed short tmpData;
            tmpData = (bufferIn[i].data[0] << 8) |
                                                            bufferIn[i].data[1];
            batteryCurrentCurrency=-(tmpData-8000)/0.125;//不对。华友要求：充电为正，放电为负
            // 电池组总电压
            tmpData = (bufferIn[i].data[2] << 8) |
                                                            bufferIn[i].data[3];
//            batterySumVoltage=tmpData/0.125;
            batterySumVoltage=tmpData*0.125;



            // 电池组剩余容量
            unsigned short tmpVolume;
            tmpVolume = (bufferIn[i].data[4] << 8) |
                                                            bufferIn[i].data[5];
            batteryLeftLightmodVolume=tmpVolume/1000.0;
            // 电池组fcc容量
            unsigned short tmpVolume_fcc;
            tmpVolume_fcc = (bufferIn[i].data[6] << 8) |
                                                            bufferIn[i].data[7];
            double fccVolume =tmpVolume_fcc/1000.0;
            batteryLeftLightmodRatio=batteryLeftLightmodVolume/fccVolume;
            isReceived=true;
        }
        else if(bufferIn[i].frameId==0x209)
        {
            batteryTempture = (bufferIn[i].data[0] ) -40;
            isReceived=true;

        }
        else
        {
//            qDebug()<<"warning, can receive unknow frameId frame!";
            continue;
        }
    }
    if(true==isReceived)
    {
        return 1;
    }
    return -1;

}
